package com.ruoyi.quartz.task;


import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sign.domain.SignIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sign.service.ISignInService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.jdbc.core.JdbcTemplate;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalTime;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;



@Component("signTask")

public class SignTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    //发布定时签到任务
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨12点触发
    public void signPublish() {
        String sql = "INSERT INTO sign_in (sign_id,user_id, date, status, check_in, sign_out)\n" +
                "SELECT NULL, user_name, CURDATE(), '正常', NULL, NULL FROM sys_user;";
        jdbcTemplate.update(sql);
    }

    // 搜索所有签到日期为当天，并且签到状态为‘正常’的记录
//    public void checkAttendance() {
//        LocalDate today = LocalDate.now();
//        String query = "SELECT * FROM sign_in WHERE date = ? AND status = '正常'";
//        List<SignIn> records = jdbcTemplate.query(query, new Object[]{today});
//
//        for (SignIn record : records) {
//            if (record.getCheckIn() == null || !isWithinTimeRange(record.getCheckIn().toLocalTime(), LocalTime.of(8, 0), LocalTime.of(10, 0))) {
//                updateStatus(record.getSignId(), "缺勤");
//            } else if (record.getSignOut() == null || Duration.between(record.getCheckIn().toLocalTime(), record.getSignOut().toLocalTime()).toHours() < 8) {
//                updateStatus(record.getSignId(), "早退");
//            }
//        }
//    }

    // 更新签到状态
    private void updateStatus(int signId, String newStatus) {
        String updateQuery = "UPDATE sign_in SET status = ? WHERE sign_id = ?";
        jdbcTemplate.update(updateQuery, newStatus, signId);
    }

    // 判断时间是否在指定范围内
    private boolean isWithinTimeRange(LocalTime time, LocalTime startTime, LocalTime endTime) {
        return !time.isBefore(startTime) && !time.isAfter(endTime);
    }

    // 定义考勤记录的映射关系



}
